"""UI状态管理器 - 统一管理界面控件的显示/隐藏状态"""


class UIStateManager:
    """统一管理UI控件状态的类"""
    
    def __init__(self, view):
        self.view = view
    
    def reset_all_controls(self):
        """重置所有控件到初始状态"""
        # 停止所有摄像头
        self._stop_all_cameras()
        
        # 隐藏所有动态控件
        self._hide_input_controls()
        self._hide_attendance_controls()
        
        # 清空预览区域
        self._clear_video_preview()
    
    def show_input_mode(self):
        """显示录入模式的控件"""
        self.reset_all_controls()
        self._show_input_controls()
    
    def show_recognition_mode(self):
        """显示识别模式的控件"""
        self.reset_all_controls()
        self._show_attendance_controls()
    
    def _stop_all_cameras(self):
        """停止所有摄像头"""
        try:
            # 停止录入摄像头
            if hasattr(self.view.controller, 'input'):
                input_controller = self.view.controller.input
                if hasattr(input_controller, 'timer') and input_controller.timer:
                    input_controller.timer.stop()
                if hasattr(input_controller, 'cap') and input_controller.cap:
                    input_controller.cap.release()
                    input_controller.cap = None
        except Exception:
            pass
        
        try:
            # 停止识别摄像头
            if hasattr(self.view.controller, 'face'):
                self.view.controller.face.stop()
        except Exception:
            pass
    
    def _show_input_controls(self):
        """显示录入相关控件"""
        controls = [
            'name_label', 'name_input', 'btn_save', 'btn_stop'
        ]
        for control_name in controls:
            if hasattr(self.view, control_name):
                getattr(self.view, control_name).setVisible(True)
    
    def _hide_input_controls(self):
        """隐藏录入相关控件"""
        controls = [
            'name_label', 'name_input', 'btn_save', 'btn_stop'
        ]
        for control_name in controls:
            if hasattr(self.view, control_name):
                control = getattr(self.view, control_name)
                control.setVisible(False)
                # 清空输入框内容
                if control_name == 'name_input' and hasattr(control, 'clear'):
                    control.clear()
    
    def _show_attendance_controls(self):
        """显示考勤相关控件"""
        controls = ['btn_clock_in', 'btn_clock_out']
        for control_name in controls:
            if hasattr(self.view, control_name):
                getattr(self.view, control_name).setVisible(True)
    
    def _hide_attendance_controls(self):
        """隐藏考勤相关控件"""
        controls = ['btn_clock_in', 'btn_clock_out']
        for control_name in controls:
            if hasattr(self.view, control_name):
                getattr(self.view, control_name).setVisible(False)
    
    def _clear_video_preview(self):
        """清空视频预览区域"""
        if hasattr(self.view, 'video_label'):
            self.view.video_label.clear()
            self.view.video_label.setText("摄像头预览将在这里显示")